package com.yystudy.system.utils;

import com.yystudy.model.system.SysDept;

import java.util.ArrayList;
import java.util.List;

public class DeptHelper {
    //对传入的数据进行格式化处理
    public static List<SysDept> buildTree(List<SysDept> sysDeptList) {
        List<SysDept> trees = new ArrayList<>();
        for (SysDept sysDept : sysDeptList) {
            if (sysDept.getParentId().longValue() == 0) {
                trees.add(findChildren(sysDept,sysDeptList));
            }
        }
        return trees;
    }


    //递归处理数据
    private static SysDept findChildren(SysDept sysDept, List<SysDept> sysDeptList) {
        sysDept.setChildren(new ArrayList<SysDept>());

        for (SysDept it : sysDeptList) {
            if(Long.parseLong(sysDept.getId()) == it.getParentId().longValue()) {
                if (sysDept.getChildren() == null) {
                    sysDept.setChildren(new ArrayList<>());
                }
                sysDept.getChildren().add(findChildren(it,sysDeptList));
            }
        }
        return sysDept;
    }

}
